﻿#include "PlatformProperty.h"

// 《DHU8_SWRA_FuncSpec_Vehicle-外灯设置.pdf》
// 3.2.9 [SW_FC_056_010]Vehicle外灯_后雾灯RearFogLight
SYSTEM_READ_PROPERTY(FOG_LIGHTS_STATE, int32_t) {
    builder.area(VehicleArea::GLOBAL)
        .initValue([]() { return static_cast<int32_t>(VehicleLightState::OFF); })
        .withStatusBySignals(
            [this]([[maybe_unused]] PropertyStatus last) {
                uint32_t afc =
                    lcfg::LocalConfig::getInstance().getInt<int32_t>(lcfg::LCFG_ReFogLi_SoftSwt_Btn);
                if (afc != 1) {
                    VHALOG.d("FOG_LIGHTS_STATE: not available, afc:%u", afc);
                    return PropertyStatus::NotAvailable;
                }

                sdb::LiExtFctReq1 li2 = SIGNAL.getSwtExtrLi2LiExtFctReq1();
                sdb::LiExtFctReq1 li3 = SIGNAL.getSwtExtrLi3LiExtFctReq1();
                int32_t req = getPropertyValue<int32_t>(
                    static_cast<int32_t>(
                        ECarXVehicleProperty::SETTING_FUNC_LAMP_EXTERIOR_LIGHT_CONTROL),
                    static_cast<int32_t>(VehicleArea::GLOBAL));

                auto usgmod = SIGNAL.getVehModMngtGlbSafe1UsgModSts();
                auto carmod = SIGNAL.getVehModMngtGlbSafe1CarModSts1();
                auto cc13 = SIGNAL.getcarconfig13();
                VHALOG.d(
                    "FOG_LIGHTS_STATE: li2:%d, req:%d, afc:%u, usgmod:%d, "
                    "carmod:%d, cc13:%d",
                    static_cast<int32_t>(li2), req, afc, usgmod, carmod, cc13);

                bool usgModeIsOK =
                    ((SIGNAL.usgModeIs(sdb::UsgModSts1::UsgModCnvinc) &&
                      SIGNAL.carConfigIs(SignalRX::carconfig13, sdb::CarConfig13::_04)) ||
                     SIGNAL.usgModeIs(sdb::UsgModSts1::UsgModActv, sdb::UsgModSts1::UsgModDrvg));

                bool extFctReqIsOk =
                    (li2 != sdb::LiExtFctReq1::Off && li2 != sdb::LiExtFctReq1::Pos) ||
                    (li3 != sdb::LiExtFctReq1::Off && li3 != sdb::LiExtFctReq1::Pos) ||
                    (req != static_cast<int32_t>(
                                ExteriorLightControlValue::LAMP_EXTERIOR_LIGHT_CONTROL_OFF) &&
                     req != static_cast<int32_t>(
                                ExteriorLightControlValue::LAMP_EXTERIOR_LIGHT_CONTROL_POS_LIGHT));

                if (SIGNAL.carModeIs(sdb::CarModSts1::CarModNorm, sdb::CarModSts1::CarModCrash,
                                     sdb::CarModSts1::CarModDyno) &&
                    usgModeIsOK && extFctReqIsOk) {
                    return PropertyStatus::Active;
                } else {
                    return PropertyStatus::NotActive;
                }
            },
            SignalRX::VehModMngtGlbSafe1UsgModSts, SignalRX::VehModMngtGlbSafe1CarModSts1,
            SignalRX::SwtExtrLi2LiExtFctReq1,SignalRX::SwtExtrLi3LiExtFctReq1)
        .withValueBySignals(
            []([[maybe_unused]] const int32_t& last) {
                sdb::Timeout reFogTimeout = SIGNAL.getExtrLtgStsTimeout();
                if (reFogTimeout == sdb::Timeout::Timeout) {
                    VHALOG.d("HalWithValueBySignals FOG_LIGHTS_STATE: timeout");
                    return last;
                }

                sdb::DevSts4 devSts4 = SIGNAL.getExtrLtgStsReFog();
                VHALOG.d("HalWithValueBySignals FOG_LIGHTS_STATE last[%d] cur[%d]", last, devSts4);

                return devSts4 == sdb::DevSts4::On ? static_cast<int32_t>(VehicleLightSwitch::ON)
                                                   : static_cast<int32_t>(VehicleLightSwitch::OFF);
            },
            SignalRX::ExtrLtgStsReFog,SignalRX::ExtrLtgStsTimeout);
}
